<script setup>
// #region 引入包, 函数和组件

// #endregion

// #region // 使用模块(仓库、路由、父组件参数函数)
const props = defineProps(["times"]);
const curIndex = defineModel("curIndex");
// #endregion

// #region // 变量

// #endregion

// #region // 函数
const formatDate = (time) => {
  // console.log(time)
  const date = new Date(time);
  // console.log(date);
  const day = date.getDate();
  const nowDay = new Date().getDate();
  // console.log(nowDay)
  let dayDf = day - nowDay;
  if (dayDf < 0) dayDf = 0
  const localeDate = date.toLocaleDateString("zh-CN", {
    month: "long",
    day: "numeric",
    weekday: "short",
  });
  const weekdayText = ["今天", "明天", "后天"];
  // console.log(dayDf)
  let res = dayDf < 3 ? weekdayText[dayDf] : localeDate.slice(-2);
  return res + localeDate.slice(0, -2);
};
// #endregion

// #region // 生命周期和vue函数

// #endregion
</script>

<template>
  <div class="timeSwitch">
    <div
      class="time"
      v-for="(item, index) in times"
      :key="item.showDate"
      :class="{ active: index === curIndex }"
      @click="curIndex = index"
    >
      {{ formatDate(item.showDate * 1000) }}
    </div>
    <div class="line" :style="{ left: curIndex + 'rem' }"></div>
  </div>
</template>

<style lang="scss" scoped>
.timeSwitch {
  overflow: hidden;
  overflow-x: auto;
  display: flex;
  height: 100%;
  border-bottom: 0.01rem solid #f7f7f7;
  font-size: 0.14rem;
  color: var(--cl-blk);
  position: relative;
  &::-webkit-scrollbar {
    display: none;
  }
  .time {
    min-width: 1rem;
    line-height: 0.48rem;
    text-align: center;
  }
  .active {
    color: #ff5f16;
  }
  .line {
    width: 1rem;
    position: absolute;
    left: 0;
    bottom: 0;
    transition: left 0.2s ease;
    &::after {
      content: "";
      position: absolute;
      left: 0.15rem;
      bottom: 0;
      // display: block;
      width: 0.7rem;
      background: #ff6c28;
      border-bottom: 0.01rem solid #f79e76;
      height: 0.01rem;
    }
  }
}
</style>
